package com.jp.knowledge.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.SkillDetailActivity;
import com.jp.knowledge.adapter.JPBaseAdpter;
import com.jp.knowledge.adapter.JPViewHolder;
import com.jp.knowledge.adapter.PositionKnowledgeAdapter;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.model.SkillModel;
import com.jp.knowledge.view.SkillContentView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen     2017-06-07 14:42:14
 *岗位知识 item  fragment
 *
 * */
public class PositionKnowleageItemFragment extends BaseFragment implements SkillContentView.OnItemClickListener {

    @ViewInject(R.id.content_view)
    private LinearLayout mView;

    @ViewInject(R.id.list_view)
    private ListView  listView;

    private PositionKnowlegeMode model;
    private boolean showImg;

    private Handler handler = new Handler();

    public PositionKnowleageItemFragment() {
    }
    public PositionKnowleageItemFragment newInstance(PositionKnowlegeMode model) {
        return  newInstance(model, false);
    }
    public PositionKnowleageItemFragment newInstance(PositionKnowlegeMode model,boolean showImg) {
        this.model=model;
        this.showImg = showImg;
        return  this;
    }

    public void updateData(PositionKnowlegeMode model){
            this.model=model;
        setView(model);
    }

    public PositionKnowlegeMode getModel(){
        return model;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View   view =  inflater.inflate(R.layout.fragment_position_knowleage_item, container, false);
        x.view().inject(this,view);
        init();
        if(showImg)
            view.findViewById(R.id.knowledge_ad_img).setVisibility(View.VISIBLE);
        return view;
    }

    private void init(){

//        setView(model);
    }

    @Override
    protected void onlazyLoad() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setView(model);
            }
        },20);

    }

    private void setView(PositionKnowlegeMode skillModel){
        if(mView!=null){
            mView.removeAllViews();
            try {
                List<PositionKnowlegeMode.ListBeanX>  listsBean = skillModel.getList();
                for(int i = 0, j = listsBean.size(); i < j; i++){
                    SkillContentView view = new SkillContentView(mContext);
                    if(i==0){
                        view.hideLine();
                    }
                    view.setData(listsBean.get(i), i);
                    view.setOnItemClickListener(this);
                    mView.addView(view);
                }
            } catch (Exception e){}
        }

    }


    /* 二级分类单击回调 */
    @Override
    public void onItemClick(int mainPosition, int subPosition) {
       List<PositionKnowlegeMode.ListBeanX> data = model.getList();
        startActivity(new Intent(mContext, SkillDetailActivity.class)
                .putExtra("data", (Serializable) data)
                .putExtra("mainPosition", mainPosition)
                .putExtra("subPosition", subPosition)
                .putExtra("id",model.getId())
                .putExtra("isPositionTool", showImg));
    }
}
